package com.wangzhixuan.apiController;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.wangzhixuan.commons.base.ApiBaseController;
import com.wangzhixuan.commons.constant.ApiCode;
import com.wangzhixuan.commons.constant.Constants;
import com.wangzhixuan.commons.scan.MyException;
import com.wangzhixuan.commons.utils.FirstWord;
import com.wangzhixuan.commons.utils.HttpClientUtil;
import com.wangzhixuan.commons.utils.ImageTools;
import com.wangzhixuan.commons.utils.LngAndLatUtil;
import com.wangzhixuan.commons.utils.UUIDGenerator;
import com.wangzhixuan.model.CArchivesImage;
import com.wangzhixuan.model.CusGroup;
import com.wangzhixuan.model.CusGroupImage;
import com.wangzhixuan.model.CusImage;
import com.wangzhixuan.model.Customer;
import com.wangzhixuan.model.EleRate;
import com.wangzhixuan.model.EleSpec;
import com.wangzhixuan.model.Metering;
import com.wangzhixuan.model.Operation;
import com.wangzhixuan.model.vo.AddCustomerVo;
import com.wangzhixuan.model.vo.CArchivesImageVo;
import com.wangzhixuan.model.vo.CusMapVo;
import com.wangzhixuan.model.vo.CustomerVo;
import com.wangzhixuan.model.vo.HightWithVo;
import com.wangzhixuan.model.vo.OpeVo;
import com.wangzhixuan.service.ICArchivesImageService;
import com.wangzhixuan.service.ICusGroupImageService;
import com.wangzhixuan.service.ICusGroupService;
import com.wangzhixuan.service.ICusImageService;
import com.wangzhixuan.service.ICustomerService;
import com.wangzhixuan.service.IEleRateService;
import com.wangzhixuan.service.IEleSpecService;
import com.wangzhixuan.service.IMeteringService;
import com.wangzhixuan.service.IOperationService;

/**
 * <p>
 *   前端控制器
 * </p>
 * @author zhixuan.wang
 * @since 2017-08-05
 */
@Controller
@RequestMapping("/api/outside")
public class CustomerApiController extends ApiBaseController {
    
    @Autowired private ICustomerService customerService;
    
    @Autowired private IEleSpecService eleSpecService;
    
    @Autowired private IEleRateService eleRateService;
    
    @Autowired private IMeteringService meteringService;
    
    @Autowired private IOperationService operationService;
    
    @Autowired private ICusImageService cusImageService;
    
    @Autowired private ICusGroupService cusGroupService;
    
    @Autowired private ICusGroupImageService cusGroupImageService;
    
    @Autowired private ICArchivesImageService cArchivesImageService;
    
    
    @RequestMapping("/add")
    @ResponseBody
    public Object add(CustomerVo customerVo) throws IllegalAccessException, InvocationTargetException, MyException {
    	customerVo.checekParam();
    	Customer customer = new Customer();
    	customer.setName(customerVo.getCusName());
    	customer.setPhone(customerVo.getCusPhoneNum());
    	customer.setCompanyName(customerVo.getCompanyName());
    	customer.setCompanyAddress(customerVo.getCompanyAddress());
    	customer.setEmail(customerVo.getCusMail());
    	customer.setWeexName(customerVo.getCusWeChat());
    	customer.setCreateId(customerVo.getUserId());
    	customer.setInitials(customerVo.getCusInitials());
    	customer.setCreateTime(new Date());
        boolean b = customerService.insert(customer);
        if (b) {
        	return renderSuccess(ApiCode.CODE_200, "成功");
        } else {
        	return renderError(ApiCode.CODE_500, "添加失败");
        }
    }

    /**
     * 编辑
     * @param 
     * @return
     * @throws InvocationTargetException 
     * @throws IllegalAccessException 
     * @throws MyException 
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(CustomerVo customerVo) throws IllegalAccessException, InvocationTargetException, MyException {
    	if(customerVo.getCusId()==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
    	customerVo.checekParam();
    	Customer customer = new Customer();
    	customer.setId(customerVo.getCusId());
    	customer.setName(customerVo.getCusName());
    	customer.setPhone(customerVo.getCusPhoneNum());
    	customer.setCompanyName(customerVo.getCompanyName());
    	customer.setCompanyAddress(customerVo.getCompanyAddress());
    	customer.setEmail(customerVo.getCusMail());
    	customer.setWeexName(customerVo.getCusWeChat());
    	customer.setCreateId(customerVo.getUserId());
    	customer.setInitials(customerVo.getCusInitials());
    	customer.setCreateTime(new Date());
        boolean b = customerService.updateById(customer);
        if (b) {
        	return renderSuccess(ApiCode.CODE_200, "成功");
        } else {
        	return renderError(ApiCode.CODE_500, "编辑失败");
        }
    }
    
    @RequestMapping("/detailed")
    @ResponseBody
    public Object detailed(Integer userId,Integer cusId) throws IllegalAccessException, InvocationTargetException, MyException {
    	if(userId==null){
			 throw new MyException(ApiCode.CODE_501, "请重新登录");
		}
    	if(cusId==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
    	Map<String ,Object> map = new HashMap<String,Object>();
    	Customer customer = customerService.selectById(cusId);
    	if(customer==null){
    		return renderError(ApiCode.CODE_500, "不存在此外部人员信息");
    	}
    	map.put("cusName", customer.getName());
    	map.put("companyName", customer.getCompanyName());
    	map.put("cusPhoneNum", customer.getPhone());
    	map.put("companyAddress", customer.getCompanyAddress()==null?"":customer.getCompanyAddress());
    	map.put("cusMail", customer.getEmail()==null?"":customer.getEmail());
    	map.put("cusWeChat", customer.getWeexName()==null?"":customer.getWeexName());
        return renderSuccess(ApiCode.CODE_200, "成功",map);
    }
    
    @RequestMapping("/list")
    @ResponseBody
    public Object list(Integer userId) throws MyException {
    	if(userId==null){
			 throw new MyException(ApiCode.CODE_501, "请重新登录");
		}
    	List<Map<String,Object>> retList = customerService.selectList(userId);	
        return renderSuccess(ApiCode.CODE_200, "成功",retList);
    }
    
    /**
     * 删除
     * @param id
     * @return
     * @throws MyException 
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object deleteCategory(Integer cusId,Integer userId) throws MyException {
    	if(cusId==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
    	if(userId==null){
			 throw new MyException(ApiCode.CODE_501, "请重新登录");
		}
        boolean b = customerService.deleteById(cusId);
        if (b) {
        	return renderSuccess(ApiCode.CODE_200, "成功");
        } else {
        	return renderError(ApiCode.CODE_500, "删除失败");
        }
    }
    
    
    /**
     * 添加规格
     * @return
     * @throws MyException
     */
    @RequestMapping("/addSpec")
    @ResponseBody
    public Object addSpec(String spec) throws MyException {
    	if(spec==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
    	Integer Id = eleSpecService.selectEnty(spec);
    	if(Id != null){  								
    		 throw new MyException(ApiCode.CODE_501, "请勿重复添加！");
    	}
    	EleSpec eleSpec = new EleSpec();
    	eleSpec.setEleSpec(spec);
    	eleSpecService.insert(eleSpec);
    	return renderSuccess(ApiCode.CODE_200, "成功");
    }
    
    /**
     * 添加倍率
     * @return
     * @throws MyException
     */
    @RequestMapping("/addRate")
    @ResponseBody
    public Object addRate(String rate) throws MyException {
    	if(rate==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
    	Integer rateId = eleRateService.selectEnty(rate);
    	if(rateId != null){  								
    		 throw new MyException(ApiCode.CODE_501, "请勿重复添加！");
    	}
    	EleRate eleRate = new EleRate();
    	eleRate.setEleRate(rate);
    	eleRateService.insert(eleRate);
    	return renderSuccess(ApiCode.CODE_200, "成功");
    }
    
    /**
     * 添加小区
     * @return
     * @throws MyException
     */
    @RequestMapping("/addCusGroup")
    @ResponseBody
    public Object addCusGroup(String cusGroup) throws MyException {
    	if(cusGroup==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
    	Integer Id = cusGroupService.selectEnty(cusGroup);
    	if(Id != null){  								
    		 throw new MyException(ApiCode.CODE_501, "请勿重复添加！");
    	}
    	CusGroup cusGroupEnty = new CusGroup();
    	cusGroupEnty.setCusGroup(cusGroup);
    	String spellscusGroup = FirstWord.getSpells(cusGroup);
		String[] xy = HttpClientUtil.getXY(cusGroup);
		cusGroupEnty.setxCode(xy[0]);
		cusGroupEnty.setyCode(xy[1]);
		cusGroupEnty.setFirstWord(spellscusGroup);
    	cusGroupService.insert(cusGroupEnty);
    	return renderSuccess(ApiCode.CODE_200, "成功");
    }
    
    /**
     * 添加计量装置
     * @return
     * @throws MyException
     */
    @RequestMapping("/addMeter")
    @ResponseBody
    public Object addMeter(String meter) throws MyException {
    	if(meter==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
    	Integer Id = meteringService.selectEnty(meter);
    	if(Id != null){  								
    		 throw new MyException(ApiCode.CODE_501, "请勿重复添加！");
    	}
    	Metering metering = new Metering();
    	metering.setMeterName(meter);
    	meteringService.insert(metering);
    	return renderSuccess(ApiCode.CODE_200, "成功");
    }
    
    public static boolean isInteger(String str) {  
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");  
        return pattern.matcher(str).matches();  
    }
    /**
     * 添加用户(户号)
     * @return
     * @throws MyException
     */
    @RequestMapping("/addCustomer")
    @ResponseBody
    public Object addCustomer(Customer customer) throws MyException {
    	if(customer.getName()==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
    	boolean bol = CustomerApiController.isInteger(customer.getName());
    	if(bol){
			 throw new MyException(ApiCode.CODE_501, "姓名必需为汉字");
		}
	   	if(customer.getPhone()==null){
				 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
			}
	   	if(customer.getCompanyAddress()==null){
				 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
			}
	   	if(customer.getRateId()==null){
				 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
			}
	   	if(customer.getSpecId()==null){
				 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
			}
	   	if(customer.getMeterId()==null){
				 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
			}
	   	if(customer.getGroupId()==null){
				 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
			}
	   	if(customer.getEleNum()==null){
				 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
			}
	   	if(customer.getAccountNumber()==null){
				 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
			}
	   	if(customer.getCreateId()==null){
				 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
			}
	   	
	   	String spellscusGroup = FirstWord.getSpells(customer.getName());
	   	customer.setInitials(spellscusGroup);
	   	customer.setCreateTime(new Date());
	   	customerService.insert(customer);
    	return renderSuccess(ApiCode.CODE_200, "成功");
    }
    
    
    /**
     * 编辑客户(小区)图片
     * @return
     * @throws Exception 
     * @throws IOException 
     */
    @RequestMapping("/editCustomer")
    @ResponseBody
    public Object editCustomer(Customer customer) throws IOException, Exception {
    	if(customer.getName()==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
	   	if(customer.getPhone()==null){
				 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
			}
	   	if(customer.getCompanyAddress()==null){
				 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
			}
	   	if(customer.getRateId()==null){
				 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
			}
	   	if(customer.getSpecId()==null){
				 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
			}
	   	if(customer.getMeterId()==null){
				 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
			}
	   	if(customer.getGroupId()==null){
				 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
			}
	   	if(customer.getEleNum()==null){
				 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
			}
	   	if(customer.getAccountNumber()==null){
				 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
			}
	   	if(customer.getCreateId()==null){
				 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
			}
	   	Customer customer2 = customerService.selectById(customer.getId());
	   	customer2.setAccountNumber(customer.getAccountNumber());
	   	customer2.setCompanyAddress(customer.getCompanyAddress());
	   	customer2.setCreateId(customer.getCreateId());
	   	customer2.setCreateTime(new Date());
	   	customer2.setEleNum(customer.getEleNum());
	   	customer2.setGroupId(customer.getGroupId());
	   	customer2.setMeterId(customer.getMeterId());
		customer2.setName(customer.getName());
		customer2.setPhone(customer.getPhone());
		customer2.setRateId(customer.getRateId());
		customer2.setSpecId(customer.getSpecId());
		String spellscusGroup = FirstWord.getSpells(customer.getName());
		customer2.setInitials(spellscusGroup);
		customerService.updateById(customer2);
        return renderSuccess(ApiCode.CODE_200, "成功");
    }
    
    /**
     * 获取规格列表
     * @return
     * @throws MyException
     */
    @RequestMapping("/listSpec")
    @ResponseBody
    public Object listSpec() throws MyException {
    	List<EleSpec> listEleSpec = eleSpecService.listEleSpec();
        return renderSuccess(ApiCode.CODE_200, "成功",listEleSpec);
    }
    
    /**
     * 获取倍率列表
     * @return
     * @throws MyException
     */
    @RequestMapping("/listRata")
    @ResponseBody
    public Object listMeter() throws MyException {
    	List<EleRate> listRate = eleRateService.listRate();
        return renderSuccess(ApiCode.CODE_200, "成功",listRate);
    }
    
    /**
     * 获取计量装置列表
     * @return
     * @throws MyException
     */
    @RequestMapping("/listMeter")
    @ResponseBody
    public Object listRata() throws MyException {
    	List<Map<String,Object>> listMeter = meteringService.listMeter();
        return renderSuccess(ApiCode.CODE_200, "成功",listMeter);
    }
    
    /**
     * 获取小区列表
     * @return
     * @throws MyException
     */
    @RequestMapping("/listCusGroupTwo")
    @ResponseBody
    public Object listCusGroup() throws MyException {
    	List<CusGroup> listCusGroup2 = cusGroupService.listCusGroup2();
    	return renderSuccess(ApiCode.CODE_200, "成功",listCusGroup2);
    	
    }
    
    
    /**
     * 获取客户群体列表
     * @return
     * @throws MyException
     */
    @RequestMapping("/cusList")
    @ResponseBody
    public Object cusList(Integer speId,Integer metId) throws MyException {
    	List<Map<String,Object>> cusGroup = cusGroupService.listCusGroup(speId, metId);
        return renderSuccess(ApiCode.CODE_200, "成功",cusGroup);
    }
    
    
    /**
     * 获取客户群体列表（地图）
     * @return
     * @throws MyException
     */
    @RequestMapping("/cusMapList")
    @ResponseBody
    public Object cusMapList(String xCode,String yCode) throws MyException {
    	List<CusMapVo> cusMapList = cusGroupService.cusMapList();
    	List<CusMapVo> list = new ArrayList<>();
    	for(CusMapVo cusMapVo:cusMapList){
    		double distance = LngAndLatUtil.getDistance(Double.parseDouble(cusMapVo.getxCode()),Double.parseDouble(cusMapVo.getyCode()) , Double.parseDouble(xCode), Double.parseDouble(yCode));
    		if(Constants.CUS_DISTANCE>distance){
    			list.add(cusMapVo);           			//筛选出三百公里内的小区
    		}
    	}
        return renderSuccess(ApiCode.CODE_200, "成功",list);
    }
    
    /**
     * 获取客户户号列表
     * @return
     * @throws MyException
     */
    @RequestMapping("/AccountList")
    @ResponseBody
    public Object AccountList(Integer cusId,Integer speId,Integer metId) throws MyException {
    	if(cusId==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
    	Map<String,Object> map = new HashMap<String, Object>();
    	Integer counts = cusGroupService.cusCounts(cusId, speId, metId);
    	List<Map<String,Object>> listCusGroup = cusGroupService.listCus(cusId, speId, metId);
    	map.put("cusList", listCusGroup);
    	map.put("queryNum", counts);
        return renderSuccess(ApiCode.CODE_200, "成功",map);
    }
    
    /**
     * 获取客户户号详情
     * @return
     * @throws MyException
     */
    @RequestMapping("/cusdetails")
    @ResponseBody
    public Object cusDetails(Integer oneCusId) throws MyException {
    	if(oneCusId==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
    	Map<String,Object> oneCusDeatile = customerService.oneCusDeatile(oneCusId);
        return renderSuccess(ApiCode.CODE_200, "成功",oneCusDeatile);
    }
    
    
    /**
     * 上传客户(小区)图片
     * @return
     * @throws Exception 
     * @throws IOException 
     */
    @RequestMapping("/groupPicture")
    @ResponseBody
    public Object groupPicture(Integer cusId,Integer userId,@RequestParam("myfiles") CommonsMultipartFile[] myfiles
    		,HttpServletRequest request, HttpServletResponse response) throws IOException, Exception {
    	if(cusId==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
    	if(userId==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
		for(MultipartFile myfile : myfiles){
        	if(myfile.isEmpty()){  
        		return renderError(ApiCode.CODE_1501, "请选择文件后上传"); 
        	}
        }
		Date date = new Date();
		List<HightWithVo> urlList = uploadFiles(myfiles, request);
		for(HightWithVo url:urlList){
			CusGroupImage cusGroupImage = new CusGroupImage();
			cusGroupImage.setCreateId(userId);
			cusGroupImage.setCreateTime(date);
			cusGroupImage.setGroupId(cusId);
			cusGroupImage.setImgPath(url.getUrl());
			cusGroupImageService.insert(cusGroupImage);
		}
        return renderSuccess(ApiCode.CODE_200, "成功");
    }
    
    /**
     * 获取客户(小区)图片
     * @return
     * @throws Exception 
     * @throws IOException 
     */
    @RequestMapping("/getGroupPicture")
    @ResponseBody
    public Object getGroupPicture(Integer cusId) throws IOException, Exception {
    	if(cusId==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
		List<String> url = cusGroupImageService.selectUrl(cusId);
        return renderSuccess(ApiCode.CODE_200, "成功",url);
    }
    
    /**
     * 运维记录列表
     * @return
     * @throws MyException
     */
    @RequestMapping("/operation")
    @ResponseBody
    public Object operation(Integer oneCusId) throws MyException {
    	if(oneCusId==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
    	List<Map<String,Object>> listOpe = operationService.listOpe(oneCusId);
        return renderSuccess(ApiCode.CODE_200, "成功",listOpe);
    }
    
    /**
     * 上传档案图片
     * @return
     * @throws Exception 
     * @throws IOException 
     */
    @RequestMapping("/archivesImage")
    @ResponseBody
    public Object archivesImage(Integer cusId,Integer userId,@RequestParam("myfiles") CommonsMultipartFile[] myfiles
    		,HttpServletRequest request, HttpServletResponse response) throws IOException, Exception {
    	if(cusId==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
    	if(userId==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
		for(MultipartFile myfile : myfiles){
        	if(myfile.isEmpty()){  
        		return renderError(ApiCode.CODE_1501, "请选择文件后上传"); 
        	}
        }
		Date date = new Date();
		List<HightWithVo> urlList = uploadFiles(myfiles, request);
		for(int i=0;i<urlList.size();i++){
			CArchivesImage cArchivesImage = new CArchivesImage();
			cArchivesImage.setCreateId(userId);
			cArchivesImage.setCreateTime(date);
			cArchivesImage.setCusId(cusId);
			cArchivesImage.setImgPath(urlList.get(i).getUrl());
    		String local = urlList.get(i).getLocal();
    		File file = new File(local);
    		float imgWidth = ImageTools.getImgWidth(file);           //获得高
    		float imgHeight = ImageTools.getImgHeight(file);		//获得宽
    		String ratio = imgWidth/imgHeight+"";
    		cArchivesImage.setRatio(ratio);
    		cArchivesImageService.insert(cArchivesImage);
		}
        return renderSuccess(ApiCode.CODE_200, "成功");
    }
    
    /**
     * 查看户号档案图片
     * @return
     * @throws Exception 
     * @throws IOException 
     */
    @RequestMapping("/findArchivesImage")
    @ResponseBody
    public Object findArchivesImage(Integer cusId) throws IOException, Exception {
    	if(cusId==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
		List<CArchivesImageVo> findArchivesImage = cArchivesImageService.findArchivesImage(cusId);
        return renderSuccess(ApiCode.CODE_200, "成功",findArchivesImage);
    }
    
    /**
     * 删除户号档案图片
     * @return
     * @throws Exception 
     * @throws IOException 
     */
    @RequestMapping("/delArchivesImage")
    @ResponseBody
    public Object delArchivesImage(Integer cusId, Integer[] imageId) throws IOException, Exception {
    	if(cusId==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
    	if(imageId==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
		for(Integer id : imageId){
			boolean b = cArchivesImageService.deleteById(id);
			if(b==false){
				return renderError(ApiCode.CODE_500, "删除失败"); 
			}
        }
        return renderSuccess(ApiCode.CODE_200, "成功");
    }
    
    
    @RequestMapping("/trueAdress")
    @ResponseBody
    public Object trueAdress(String xCode,String yCode,Integer groudId) throws IOException, Exception {
    	if(xCode==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
    	if(yCode==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
    	if(groudId==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
    	CusGroup cusGroup = cusGroupService.selectById(groudId);
    	cusGroup.setxCode(xCode);
    	cusGroup.setyCode(yCode);
    	cusGroupService.updateById(cusGroup);
        return renderSuccess(ApiCode.CODE_200, "成功");
    }
    /**
     * 添加运维记录
     * @return
     * @throws Exception 
     * @throws IOException 
     */
    @RequestMapping("/addOpe")
    @ResponseBody
    public Object addOperation(OpeVo opeVo,@RequestParam("myfiles") CommonsMultipartFile[] myfiles,HttpServletRequest request, HttpServletResponse response) throws IOException, Exception {
    	opeVo.checekParam();
		for(MultipartFile myfile : myfiles){
        	if(myfile.isEmpty()){  
        		return renderError(ApiCode.CODE_1501, "请选择文件后上传"); 
        	}
        }
		Date date = new Date();
    	Operation operation = new Operation();
    	operation.setAddress(opeVo.getAddress());
    	operation.setContent(opeVo.getContent());
    	operation.setCreateId(opeVo.getUserId());
    	operation.setCreateTime(date);
    	operation.setCusId(opeVo.getCusId());
    	operation.setxCode(opeVo.getxCode());
    	operation.setyCode(opeVo.getyCode());
    	operationService.insert(operation);
    	Integer opeId = operation.getId();
		List<HightWithVo> urlList = uploadFiles(myfiles, request);
		for(int i=0;i<urlList.size();i++){
    		CusImage cusImage = new CusImage();
    		cusImage.setCreateId(opeVo.getUserId());
    		cusImage.setCreateTime(date);
    		cusImage.setImgPath(urlList.get(i).getUrl());
    		cusImage.setOpeId(opeId);
    		String local = urlList.get(i).getLocal();
    		File file = new File(local);
    		float imgWidth = ImageTools.getImgWidth(file);           //获得高
    		float imgHeight = ImageTools.getImgHeight(file);		//获得宽
    		String ratio = imgWidth/imgHeight+"";
    		cusImage.setRatio(ratio);
    		cusImageService.insert(cusImage);
		}
        return renderSuccess(ApiCode.CODE_200, "成功");
    }
    
    private List<HightWithVo> uploadFiles(MultipartFile[] myfiles, HttpServletRequest request)
			throws Exception, IOException {
		String pathRoot = getRootPath();
		String filePath = mkdirs(pathRoot);
		new File(filePath).mkdirs();
		List<HightWithVo> list = new ArrayList<HightWithVo>();
		for(MultipartFile myfile : myfiles){
			System.out.println(myfile.getOriginalFilename());
			String fileType = myfile.getOriginalFilename().substring(
					myfile.getOriginalFilename().lastIndexOf("."));
			String resId = UUIDGenerator.getUUID();
			String tempfilePath = filePath + resId + fileType;
			myfile.transferTo(new File(pathRoot + tempfilePath));
			HightWithVo hightWithVo = new HightWithVo();
			hightWithVo.setLocal(pathRoot + tempfilePath);
			hightWithVo.setUrl(getbaseUrl(request) + tempfilePath);
			list.add(hightWithVo);
		}
		return list;
	}

	private String mkdirs(String pathRoot) {
		Date date = new Date();
		StringBuffer path = new StringBuffer();
		path.append(new SimpleDateFormat("yyyy/MM/dd").format(date))
				.append("/");
		File f = new File(pathRoot + path.toString());
		if (!f.exists())
			f.mkdirs();
		return path.toString();
	}
    
}
